package cloud.tianai.ram.policy.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 天爱有情
 * @date 2022/3/25 10:18
 * @Description 自用相关字符串工具包
 */
public class RamStringUtils {

    /**
     * 提取字符串中的占位符数据
     * @param str 字符串
     * @param left 占位符 left
     * @param right 占位符 right
     * @return List<String>
     */
    public static List<String> extract(String str, char left, char right) {
        if (StringUtils.isEmpty(str)) {
            return Collections.emptyList();
        }
        List<String> word = new LinkedList<>();
        int m = 0, n;
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == left) {
                if (count == 0) {
                    m = i;
                }
                count++;
            }
            if (str.charAt(i) == right) {
                count--;
                if (count == 0) {
                    n = i;
                    word.add(str.substring(m, n + 1));
                }
            }
        }
        return word;
    }

    public static void main(String[] args) {
        String str="您好{options{abc}}评论了您的{options{def}}";
        List<String> extract = extract(str, '{', '}');
        System.out.println(extract);
    }

}
